package com.springcloud.sc.sso.controller;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.ConnectException;
import java.util.List;
import java.util.Map;

@RestController
public class SsoController {

    @GetMapping("/user")
    public Authentication user(Authentication authentication) {
        return authentication;
    }


    //单点登录对接例子
    @GetMapping("/user2")
    public String getValue(Authentication authentication){
        OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails)authentication.getDetails();
        System.out.println(oAuth2AuthenticationDetails.getTokenValue());
        String tokenValue = oAuth2AuthenticationDetails.getTokenValue();
        String tokenType = oAuth2AuthenticationDetails.getTokenType();
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        String url = "http://127.0.0.1:9999/admin/dict/page";
        String result = "";
        // 创建httpClient实例
        httpClient = HttpClients.createDefault();
        // 创建 HttpGet 远程连接实例
        HttpGet httpGet = new HttpGet(url);
        // 配置请求参数实例
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间
                .setConnectionRequestTimeout(35000)// 设置连接请求超时时间
                .setSocketTimeout(60000)// 设置读取数据连接超时时间
                .build();
        // 为httpPost实例设置配置
        httpGet.setConfig(requestConfig);
        // 设置请求头
        httpGet.addHeader("Content-Type", "application/json");
        httpGet.addHeader("Authorization", tokenType+" "+ tokenValue );
        /*// 封装json请求参数
        try {
            StringEntity requestEntity = new StringEntity(json,"utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setEntity( requestEntity );
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        try {
            // httpClient对象执行post请求,并返回响应参数对象
            httpResponse = httpClient.execute(httpGet);
            // 从响应对象中获取响应内容
            HttpEntity entity = httpResponse.getEntity();
            result = EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }catch(ConnectException connectException){
            System.out.println("Web接口连接异常.");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != httpResponse) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}
